﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Axis.Web.Administrator.Global;

namespace Axis.Web.Views.Captcha
{
    /// <summary>
    /// Summary description for ImageCaptcha
    /// </summary>
    public class ImageCaptcha : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private Random rand = new Random();
        int width = 200;
        int height = 50;
        public void ProcessRequest(HttpContext context)
        {
            int r = rand.Next(0, 2);
            context.Response.ContentType = "image/jpeg";
            string code = Captcha.generateRandomCode(6);
            /* Save captcha code to Session */
            context.Session["CAPTCHA_CODE"] = CSecurity.GetMD5HashData(code);
            r = 1;
            if (r == 1)
            {
                Captcha captcha = new Captcha(code, width, height);
                context.Response.Clear();

                captcha.Image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
                captcha.Dispose();
            }
            else
            {
                CreateImage(code, context);
            }
        }
        private string GetRandomText()
        {
            return Captcha.generateRandomCode();
        }
        private Color RandomColor(int r)
        {
            //int r = rand.Next(1, 16);
            switch (r)
            {
                case 0:
                    return Color.FromKnownColor(KnownColor.Red);
                case 1:
                    return Color.FromKnownColor(KnownColor.White);
                case 2:
                    return Color.FromKnownColor(KnownColor.Yellow);
                case 3:
                    return Color.FromKnownColor(KnownColor.White);
                case 4:
                    return Color.FromKnownColor(KnownColor.Red);
                case 5:
                    return Color.FromKnownColor(KnownColor.Yellow);
                default:
                    return Color.FromKnownColor(KnownColor.Red);
            }
        }
        private Font RandomFont()
        {
            string[] fonts = { "Helvetica", "Arial", "DejaVu Sans", "Liberation Sans", "Freesans", "sans-serif" };
            string FontName = fonts[rand.Next(0, fonts.Length)];
            Font font = new Font(FontName, 10 + rand.Next(10, 20), FontStyle.Bold);
            return font;
        }
        private void CreateImage(string code, HttpContext context)
        {
            Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics g = Graphics.FromImage(bitmap);
            Pen pen = new Pen(Color.Yellow);
            Rectangle rect = new Rectangle(0, 0, width, height);
            SolidBrush b = new SolidBrush(ColorTranslator.FromHtml("#000"));
            int counter = 0;
            g.DrawRectangle(pen, rect);
            g.FillRectangle(b, rect);
            for (int i = 0; i < code.Length; i++)
            {
                SolidBrush brush = new SolidBrush(RandomColor(i));
                Font font = RandomFont();
                g.DrawString(code[i].ToString(), font, brush, new PointF(10 + counter, 10));
                counter += 20;
            }
            bitmap.Save(context.Response.OutputStream, ImageFormat.Gif);
            g.Dispose();
            bitmap.Dispose();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

}